// pages/coupons/coupons.js
const app = getApp()
const WXAPI = require("../../api/api")
Page({
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '优惠券', // 导航栏 中间的标题
      textColor: '#333', // 标题颜色
      bgColor: '#FAFAFA', // 导航栏背景颜色
      isloading: true,
      isColor: true
    },
    navList:[
      {text:"可使用优惠券",value: ''},
      {text:"不可使用优惠券",value: ''},
    ],
    currentNavtab: 0,
    isBiao: false,
    merchantId: '',
    payAmount: '',
    vouchersList: [],//可用优惠券
    ablesNum: '',
    unAblesNum: '',
    preferentialMoney: '',//优惠价格
    couponId: '',//优惠券ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var payAmount = options.payAmount
    var merchantId = options.id
    this.setData({
      payAmount: payAmount,
      merchantId: merchantId
    })
    this.getConfirmInfo()
  },
  //获取优惠券
  getConfirmInfo(){
    const that = this
    var data = {
      // merchantId:this.data.merchantId,
      // payAmount: this.data.payAmount
      merchantId:'1331145556510842881',
      payAmount: 1111
    }
    WXAPI.customerMerchantTickets(data, (res) =>{
      const data = res.result
      var vouchersList = this.data.vouchersList
      console.log("获取优惠券",res)
      if(res.code === 200){
        if(this.data.currentNavtab == 0){
          this.setData({
            vouchersList: data.ables,
          })
        }else if(this.data.currentNavtab == 1){
          this.setData({
            vouchersList: data.unAbles,
          })
        }
        console.log("vouchersList",this.data.vouchersList)
        this.setData({
          ['navList[0].value']: res.result.ables.length,
          ['navList[1].value']: res.result.unAbles.length,
        })
      }
    })
  },
  tabClick(e){
    console.log(e.currentTarget.dataset.idx)
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx,
    })
    this.getConfirmInfo()
  },
  handleBiao(e){
    console.log(this.data.isBiao)
    var isBiao = this.data.isBiao
    var money = e.currentTarget.dataset.money
    var id = e.currentTarget.dataset.id
    this.setData({
      isBiao: !this.data.isBiao
    })
    if(isBiao){
      this.setData({
        preferentialMoney: 0,
      })
    }else{
      this.setData({
        preferentialMoney: money,
      })
    }
    
    
  },
  showDialog() {
    // this.dialog.showDialog();
    var preferentialMoney = this.data.preferentialMoney;
    var couponId = this.data.couponId;
    wx.navigateTo({
      url: '/pages/pay/pay?preferentialMoney='+preferentialMoney+'&couponId='+couponId,
    })
  },
  //取消事件 
  _cancelEvent() {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  }, 
  //确认事件 
  _confirmEvent() {
    console.log('你点击了确定');
    this.dialog.hideDialog();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.dialog = this.selectComponent("#dialog"); 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})